home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CBASE102.ARJ / BTOPEN.C < prev    next >
Text File  |  1991-09-23  |  4KB  |  157 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)btopen.c    1.5 - 91/09/23" */
  5.  
  6. #include <ansi.h>
  7.  
  8. /* ansi headers */
  9. #include <errno.h>
  10. #ifdef AC_STDDEF
  11. #include <stddef.h>
  12. #endif
  13. #ifdef AC_STDLIB
  14. #include <stdlib.h>
  15. #endif
  16. #ifdef AC_STRING
  17. #include <string.h>
  18. #endif
  19.  
  20. /* library headers */
  21. #include <blkio.h>
  22.  
  23. /* local headers */
  24. #include "btree_.h"
  25.  
  26. /* btree control structure table definition */
  27. btree_t btb[BTOPEN_MAX];
  28.  
  29. /*man---------------------------------------------------------------------------
  30. NAME
  31.      btopen - open a btree
  32.  
  33. SYNOPSIS
  34.      btree_t *btopen(filename, type, fldc, fldv)
  35.      const char *filename;
  36.      const char *type;
  37.      int fldc;
  38.      const btfield_t fldv[];
  39.  
  40. DESCRIPTION
  41.      The btopen function opens the file named by filename as a btree.
  42.      A pointer to the btree_t control structure associated with the
  43.      file is returned.
  44.  
  45.      type is a character string having one of the following values:
  46.  
  47.           "r"            open for reading
  48.           "r+"           open for update (reading and writing)
  49.  
  50.      See btcreate for explanation of the field count fldc and the
  51.      field definition list fldv.
  52.  
  53.      btopen will fail if one or more of the following is true:
  54.  
  55.      [EINVAL]       filename is the NULL pointer.
  56.      [EINVAL]       type is not "r" or "r+".
  57.      [EINVAL]       fldc is less than 1.
  58.      [EINVAL]       fldv is the NULL pointer.
  59.      [EINVAL]       fldv contains an invalid field definition.
  60.      [ENOENT]       The named btree file does not exist.
  61.      [BTEMFILE]     Too many open btrees.  The maximum is defined as
  62.                     BTOPEN_MAX in <btree.h>.
  63.  
  64. SEE ALSO
  65.      btclose, btcreate.
  66.  
  67. DIAGNOSTICS
  68.      On failure btopen returns a NULL pointer, and errno is set to
  69.      indicate the error.
  70.  
  71. NOTES
  72.      The same field count and field definition list with which the
  73.      btree was created must be used each time the btree is opened.
  74.      Otherwise the results are undefined.
  75.  
  76. ------------------------------------------------------------------------------*/
  77. #ifdef AC_PROTO
  78. btree_t *btopen(const char *filename, const char *type, int fldc, const btfield_t fldv[])
  79. #else
  80. btree_t *btopen(filename, type, fldc, fldv)
  81. const char *filename;
  82. const char *type;
  83. int fldc;
  84. const btfield_t fldv[];
  85. #endif
  86. {
  87.     btree_t *    btp    = NULL;
  88.     int        terrno    = 0;        /* tmp errno */
  89.  
  90.     /* validate arguments */
  91.     if (filename == NULL || type == NULL) {
  92.         errno = EINVAL;
  93.         return NULL;
  94.     }
  95.  
  96.     /* find free slot in btb table */
  97.     for (btp = btb; btp < btb + BTOPEN_MAX; ++btp) {
  98.         if (!(btp->flags & BTOPEN)) {
  99.             break;        /* found */
  100.         }
  101.     }
  102.     if (btp >= btb + BTOPEN_MAX) {
  103.         errno = BTEMFILE;    /* no free slots */
  104.         return NULL;
  105.     }
  106.  
  107.     /* open file */
  108.     if (strcmp(type, BT_READ) == 0) {
  109.         btp->flags = BTREAD;
  110.     } else if (strcmp(type, BT_RDWR) == 0) {
  111.         btp->flags = BTREAD | BTWRITE;
  112.     } else {
  113.         errno = EINVAL;
  114.         return NULL;
  115.     }
  116.     btp->bp = bopen(filename, type, sizeof(bthdr_t), (size_t)1, (size_t)0);
  117.     if (btp->bp == NULL) {
  118.         if ((errno != ENOENT) && (errno != EACCES)) BTEPRINT;
  119.         terrno = errno;
  120.         memset(btp, 0, sizeof(*btb));
  121.         btp->flags = 0;
  122.         errno = terrno;
  123.         return NULL;
  124.     }
  125.  
  126.     /* load btree_t structure */
  127.     memset(&btp->bthdr, 0, sizeof(btp->bthdr));    /* header */
  128.     if (!bt_fvalid(UINT_MAX, fldc, fldv)) {
  129.         bclose(btp->bp);
  130.         memset(btp, 0, sizeof(*btb));
  131.         btp->flags = 0;
  132.         errno = EINVAL;
  133.         return NULL;
  134.     }
  135.     btp->fldc = fldc;        /* field count */
  136.     btp->fldv = NULL;
  137.     btp->cbtpos.node = NIL;        /* cursor */
  138.     btp->cbtpos.key = 0;
  139.     btp->cbtnp = NULL;
  140.     btp->sp = NULL;
  141.  
  142.     /* copy field definition array */
  143.     btp->fldv = (btfield_t *)calloc((size_t)btp->fldc, sizeof(*btp->fldv));
  144.     if (btp->fldv == NULL) {
  145.         BTEPRINT;
  146.         terrno = errno;
  147.         bclose(btp->bp);
  148.         memset(btp, 0, sizeof(*btp));
  149.         btp->flags = 0;
  150.         errno = terrno;
  151.         return NULL;
  152.     }
  153.     memcpy(btp->fldv, fldv, btp->fldc * sizeof(*btp->fldv));
  154.  
  155.     return btp;
  156. }
  157.